<!--
 * @Descripttion: 系统计划任务配置
 * @version: 1.2
 * @Author: sakuya
 * @Date: 2021年7月7日09:28:32
 * @LastEditors: sakuya
 * @LastEditTime: 2021年7月10日20:56:47
-->

<template>
  <el-dialog :title="titleMap[mode]" v-model="visible" :width="450" destroy-on-close @closed="$emit('closed')">
    <el-form :model="form" :rules="rules" ref="dialogForm" label-width="80px" label-position="left">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="模型" prop="model">
        <el-select v-model="form.model" filterable style="width: 100%" :allow-create="true">
          <el-option-group v-for="group in modelList" :key="group.code" :label="group.name">
            <el-option v-for="item in group.models" :key="item" :label="item" :value="item" />
          </el-option-group>
        </el-select>
      </el-form-item>
      <el-form-item label="嵌入模型" prop="embeddingModel">
        <el-select v-model="form.embeddingModel" filterable style="width: 100%" :allow-create="true">
          <el-option-group v-for="group in modelList" :key="group.code" :label="group.name">
            <el-option v-for="item in group.models" :key="item" :label="item" :value="item" />
          </el-option-group>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import scCron from '@/components/scCron';

export default {
  name: 'AIKnowledgeManageSave',
  components: {
    scCron,
  },
  emits: ['success', 'closed'],
  data() {
    return {
      mode: 'add',
      titleMap: {
        add: '新增知识库',
        edit: '编辑知识库',
      },
      modelList: [],
      form: {
        name: '',
        model: '',
        embeddingModel: '',
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur',
          },
        ],
        model: [
          {
            required: true,
            message: '请选择模型',
            trigger: 'blur',
          },
        ],
        embeddingModel: [
          {
            required: true,
            message: '请选择嵌入模型',
            trigger: 'blur',
          },
        ],
      },
      visible: false,
      isSaveing: false,
    };
  },
  async mounted() {},
  methods: {
    async loadAIModels() {
      let response = await this.$API.aiModel.list.get('');

      this.modelList = response.data;
    },
    //显示
    open(mode = 'add') {
      this.mode = mode;
      this.loadAIModels();
      this.visible = true;
      return this;
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async valid => {
        if (valid) {
          this.isSaveing = true;
          var res;
          if (this.form.id == null || this.form.id == '') {
            res = await this.$API.aiWiki.create.post(this.form);
          } else {
            res = await this.$API.aiWiki.update.post(this.form);
          }
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit('success');
            this.visible = false;
            this.$message.success('操作成功');
          } else {
            this.$alert(res.message, '提示', { type: 'error' });
          }
        }
      });
    },
    //表单注入数据
    setData(data) {
      let deepCopiedObject = JSON.parse(JSON.stringify(data));
      this.form = deepCopiedObject;
    },
  },
};
</script>

<style></style>
